package com.woyowodaows.activity.mine;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.woyowodaows.R;
import com.woyowodaows.activity.home.LoginActivity;
import com.woyowodaows.application.MyApplication;
import com.woyowodaows.base.BaseActivity;
import com.woyowodaows.base.BaseBean;
import com.woyowodaows.bean.mine.UserInfoBean;
import com.woyowodaows.dialog.YNDialog;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.ActivityManagerUtils;
import com.woyowodaows.utils.CacheUserInfoUtils;
import com.woyowodaows.utils.JpushTagData;
import com.woyowodaows.utils.LoginJudge;
import com.woyowodaows.utils.SPUtils;
import com.woyowodaows.utils.ToastUtil;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by zys  on 2018/7/27.
 * 账户安全
 */

public class AccountSecurityActivity extends BaseActivity {

    @BindView(R.id.tv_account_seciroty_clear)
    TextView tvAccountSecirotyClear;
    @BindView(R.id.tv_account_seciroty_change_password)
    TextView tvAccountSecirotyChangePassword;
    @BindView(R.id.btn_account_security_exit)
    Button btnAccountSecurityExit;
    @BindView(R.id.tv_account_information)
    TextView tvAccountInformation;//版本信息

    private YNDialog clearCacheDialog;//清楚缓存
    private YNDialog exitDialog;//退出登录

    @Override
    public int getLayoutId() {
        return R.layout.activity_account_security;
    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);
        ActivityManagerUtils.getInstance().addActivity(AccountSecurityActivity.this);
        setTitleBar(false, "设置", "", 0, 0, null);
        tvAccountInformation.setText("当前版本"+getLocalVersionName());
    }
    //获取版本名称
    public String getLocalVersionName() {
        String localName = "";
        try {
            PackageInfo packageInfo = mContext.getApplicationContext().getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
            localName = packageInfo.versionName;
            //   LogUtils.d("版本号",localName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return localName;
    }
    private void deleteFile(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
                deleteFile(f);
            }
            file.delete();//如要保留文件夹，只删除文件，请注释这行
        } else if (file.exists()) {
            file.delete();
        }
    }

    @OnClick({R.id.tv_account_seciroty_clear, R.id.tv_account_seciroty_change_password, R.id.btn_account_security_exit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_account_seciroty_clear://清除缓存
                clearCacheDialog = new YNDialog(this);
                if (clearCacheDialog == null) {
                    clearCacheDialog.setDialogListener(new YNDialog.OnDialogListener() {
                        @Override
                        public void onCancelClick(View view) {
                        }

                        @Override
                        public void onSureClick(View view) {
                            String path = getApplicationContext().getCacheDir() + "/glide_cache";
                            File file = new File(path);
                            if (file.exists()) {
                                deleteFile(file);
                            } else {

                            }
                            Glide.get(AccountSecurityActivity.this).clearMemory();  //主线程执行
//                            WebViewUtil.clearCache();
                            ToastUtil.show(mContext, "清除完成");
                        }
                    });
                }
                clearCacheDialog.setTitleText("确定清除？");
                clearCacheDialog.show();
                break;
            case R.id.tv_account_seciroty_change_password://修改密码
                startActivity(new Intent(mContext,  EditPassworddActivity.class));
                break;
            case R.id.btn_account_security_exit://退出登录
                if (exitDialog == null) {
                    exitDialog = new YNDialog(this);
                    exitDialog.setDialogListener(new YNDialog.OnDialogListener() {
                        @Override
                        public void onCancelClick(View view) {

                        }

                        @Override
                        public void onSureClick(View view) {
                            exitLogin();
                        }
                    });
                }
                exitDialog.setTitleText("确定退出账户？");
                exitDialog.show();
                break;
        }
    }



    private void exitLogin() {
        Map<String, Object> mapParameter = new HashMap<>();
        HttpAsyncTask.getInstance().onPostParam(this, "正在退出登录...", true, InterfaceUrl.LOGOUT, BaseBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    BaseBean bean = (BaseBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        UserInfoBean userInfoBean=CacheUserInfoUtils.getUserInfo();
                        String distributionAreaIds=userInfoBean.getDistributionAreaIds();
                        if(distributionAreaIds!=null && !"".equals(distributionAreaIds)) {
                            Set<String> tags = new HashSet<String>();
                            String[] sourceStrArray = distributionAreaIds.split(",");
                            for (int i = 0; i < sourceStrArray.length; i++) {
                                tags.add(sourceStrArray[i]);
                            }
//                            JPushInterface.setTags(mContext, tags,new TagAliasCallback(){
//
//                                @Override
//                                public void gotResult(int i, String s, Set<String> set) {
//                                    System.out.println(s+i+"----");
//                                }
//                            });

                            JPushInterface.getAlias(mContext,202);
                            JPushInterface.getAllTags(mContext,2);
//                            JPushInterface.deleteTags(mContext, 201, tags);
                            JPushInterface.cleanTags(mContext, 2);
                            JPushInterface.deleteTags(mContext,2,tags);
                            JPushInterface.cleanTags(mContext,-1);
                        }
                            // 退出登录删除别名
                            JPushInterface.deleteAlias(mContext,202);
                            LoginJudge.clearData();
                            CacheUserInfoUtils.setUserInfo(null);//清空数据库数据
                            SPUtils.remove(MyApplication.getApplicationContext(), SPUtils.IS_LOGIN);
                            SPUtils.remove(MyApplication.getApplicationContext(), SPUtils.REQUEST_HEAD_JWT);
                            LoginJudge.isLogin(AccountSecurityActivity.this, LoginActivity.class, null);
                            ActivityManagerUtils.getInstance().finishAllActivity();
                            finish();
                        } else {
                            ToastUtil.show_short(mContext, bean.getMessage());
                        }
                    }
                }

                @Override
                public void onError(String message) {
                    ToastUtil.show_short(mContext, message);
                }
            });
        }


    }

